package com.crm.CLdriving.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.plugins.Page;
import com.crm.CLdriving.common.PageResponse;
import com.crm.CLdriving.dto.PageReq.PageNumber;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.crm.CLdriving.common.BaseResponse;
import com.crm.CLdriving.dto.IdReqDto;
import com.crm.CLdriving.dto.req.ExaminInputReqDto;
import com.crm.CLdriving.dto.req.ExaminInsertReqDto;
import com.crm.CLdriving.dto.req.ExaminUpdateReqDto;
import com.crm.CLdriving.enu.ResponseEnum;
import com.crm.CLdriving.mapper.ExaminMapper;
import com.crm.CLdriving.mapper.SubFourMapper;
import com.crm.CLdriving.mapper.SubOneMapper;
import com.crm.CLdriving.mapper.SubThreeMapper;
import com.crm.CLdriving.mapper.SubTowMapper;
import com.crm.CLdriving.po.ExaminPO;
import com.crm.CLdriving.po.SubjectFourPO;
import com.crm.CLdriving.po.SubjectOnePO;
import com.crm.CLdriving.po.SubjectThreePO;
import com.crm.CLdriving.po.SubjectTowPO;
import com.crm.CLdriving.service.ExaminService;

import lombok.extern.log4j.Log4j2;

@Transactional
@Service
@Log4j2
public class ExaminServiceImpl implements ExaminService {

	@Autowired
	private ExaminMapper examinMapper;
	
	@Autowired
	private SubOneMapper subOneMapper;
	@Autowired
	private SubTowMapper subTowMapper;
	@Autowired
	private SubThreeMapper subThreeMapper;
	@Autowired
	private SubFourMapper subFourMapper;
	@Value("${data.pageSize.examin}")
	private  int examinSize;
	/**
	 * 查询  (所有学员) 考试成绩信息
	 */
	@Override
	public PageResponse<List<ExaminPO>> selectAll(PageNumber pageNumber) {
		Page<ExaminPO> page = new Page<>(pageNumber.getPageNumber(),examinSize);
		List<ExaminPO> list = examinMapper.selectPage(page,null);
		//List<ExaminPO> list = examinMapper.selectList(null);
		if (!"[]".equals(list.toString())) {
			log.info("查询  (所有学员) 考试成绩信息："+list.toString());
			return PageResponse.successOfPage(list,page);
		}
		return PageResponse.message(ResponseEnum.ZWSJ);
	}

	/**
	 * 通过 身份证号 判断是否已经录入
	 * 录入 考试成绩信息
	 */
	@Override
	public BaseResponse<?> insert(ExaminInsertReqDto examinInsertReqDto) {
		EntityWrapper<ExaminPO> wrapper = new EntityWrapper<>();
		wrapper.eq("identity", examinInsertReqDto.getIdentity());
		List<ExaminPO> list = examinMapper.selectList(wrapper);
		if (!"[]".equals(list.toString())) {
			log.info("已经录入考试成绩信息");
			return BaseResponse.message(ResponseEnum.YJLR);
		}
		ExaminPO examinPO = new ExaminPO();
		try {		
			BeanUtils.copyProperties(examinPO, examinInsertReqDto);
			log.info("copy后："+examinPO);
		} catch (Exception e) {
			log.info("copy:"+e);
		}
		Integer row = examinMapper.insert(examinPO);
		if (row == 1) {
			log.info("录入考试成绩信息成功");
			return BaseResponse.message(ResponseEnum.LRCG);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	/**
	 * 通过id
	 * 更新考试成绩信息
	 */
	@Override
	public BaseResponse<?> update(ExaminUpdateReqDto examinUpdateReqDto) {
		ExaminPO examinPO = new ExaminPO();
		try {
			BeanUtils.copyProperties(examinPO, examinUpdateReqDto);
		} catch (Exception e) {
			log.info(e);
		}
		Integer row = examinMapper.updateById(examinPO);
		if (row == 1) {
			log.info("更新考试成绩信息成功");
			return BaseResponse.message(ResponseEnum.GXCG);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	@Override
	public BaseResponse<List<ExaminPO>> selectInput(ExaminInputReqDto examinInputReqDto) {
		EntityWrapper<ExaminPO> wrapper = new EntityWrapper<>();
		if (examinInputReqDto.getIdentity() != null && examinInputReqDto.getIdentity() != "") {
			wrapper.eq("identity", examinInputReqDto.getIdentity());
		}
		if (examinInputReqDto.getName() != null && examinInputReqDto.getName() != "") {
			wrapper.like("name", examinInputReqDto.getName());
		}	
		List<ExaminPO> list = examinMapper.selectList(wrapper);
		if (!"[]".equals(list.toString())) {
			log.info("筛选框查询结果："+list.toString());
			return BaseResponse.successOf(list);
		}
		return BaseResponse.successOf(null);
	}

	@Override
	public BaseResponse<?> deleteById(IdReqDto idReqDto) {
		ExaminPO examinPO = examinMapper.selectById(idReqDto.getId());
		if (examinPO != null) {
			String identity = examinPO.getIdentity();
		    subOneMapper.delete(new EntityWrapper<SubjectOnePO>().eq("identity", identity));
		    subTowMapper.delete(new EntityWrapper<SubjectTowPO>().eq("identity", identity));
			subThreeMapper.delete(new EntityWrapper<SubjectThreePO>().eq("identity", identity));
			subFourMapper.delete(new EntityWrapper<SubjectFourPO>().eq("identity", identity));
			
			Integer row = examinMapper.deleteById(idReqDto.getId());
			if (row == 1) {
				log.info("删除考试信息成功");
				return BaseResponse.message(ResponseEnum.SQCG);
			}
		}	
		return BaseResponse.message(ResponseEnum.FAIL);
	}
	

}
